import 'dart:async';

import 'package:chatbot/manager/event_bus.dart';
import 'package:chatbot/manager/store_manager.dart';
import 'package:chatbot/model/event.dart';
import 'package:chatbot/page/fuwu.dart';
import 'package:chatbot/page/privacy_page.dart';
import 'package:chatbot/page/vip_page.dart';
import 'package:chatbot/utils/colors.dart';
import 'package:chatbot/utils/utils.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:share_plus/share_plus.dart';

class MinePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return MineState();
  }
}

class MineState extends State<MinePage> {
  late StreamSubscription eventSubscription,eventRestoreSubscription;

  @override
  void initState() {
    super.initState();
    eventSubscription = EventManager.instance.eventBus
        .on<PurchaseUpdateEvent>()
        .listen((event) {
      debugPrint("PurchaseSuccessEvent");
      if (mounted) {
        setState(() {});
      }
    });

    eventRestoreSubscription = EventManager.instance.eventBus
        .on<RestoreUpdateEvent>()
        .listen((event) {
      debugPrint("PurchaseSuccessEvent");
      if (mounted) {
        setState(() {});
      }
    });
  }
  @override
  void dispose() {
    super.dispose();
    eventSubscription.cancel();
    eventRestoreSubscription.cancel();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: ChatColors.lightColor,
      appBar: AppBar(
          backgroundColor: ChatColors.lightColor,
          title: const Text(
            "我的",
            style: TextStyle(
                color: Colors.black, fontSize: 16, fontWeight: FontWeight.w600),
          )),
      body: SingleChildScrollView(
        child: Column(
          children: [
            StoreManager.isVip()
                ? Container()
                : InkWell(
                    highlightColor: Colors.transparent, // 透明色
                    splashColor: Colors.transparent, // 透明色
                    onTap: () {
                      Navigator.push(context,
                          CupertinoPageRoute(builder: (BuildContext context) {
                        return VipPage("我的");
                      }));
                    },
                    child: Container(
                      alignment: Alignment.center,
                      width: MediaQuery.of(context).size.width - 32,
                      height: 58,
                      margin: const EdgeInsets.only(bottom: 16),
                      decoration: const BoxDecoration(
                        gradient: LinearGradient(
                          colors: [Color(0xFFfccbad), Color(0xFFffe7d6)],
                          begin: Alignment.centerLeft,
                          end: Alignment.centerRight,
                        ),
                        borderRadius: BorderRadius.all(Radius.circular(12.0)),
                      ),
                      child: Row(
                        children: [
                          const Padding(
                            padding: EdgeInsets.only(left: 6.0, right: 6),
                            child: Icon(
                              Icons.workspace_premium,
                              size: 26,
                              color: Color(0xFF313e70),
                            ),
                          ),
                          const Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text(
                                "高级VIP",
                                style: TextStyle(
                                    color: Color(0xFF313e70),
                                    fontSize: 17,
                                    fontWeight: FontWeight.bold),
                              ),
                              Text(
                                "更快相应，更智能，无限次数",
                                style: TextStyle(
                                    color: Color(0xFF313e70),
                                    fontSize: 12,
                                    fontWeight: FontWeight.w500),
                              ),
                            ],
                          ),
                          Expanded(child: Container()),
                          Container(
                              alignment: Alignment.center,
                              width: 72,
                              height: 32,
                              margin: const EdgeInsets.only(right: 8),
                              decoration: const BoxDecoration(
                                color: Color(0xFF343541),
                                borderRadius: BorderRadius.all(
                                    Radius.circular(16.0)),
                              ),
                              child: const Text(
                                "立即开通",
                                style: TextStyle(
                                    color: Colors.white,
                                    fontSize: 12,
                                    fontWeight: FontWeight.bold),
                              ))
                        ],
                      ),
                    ),
                  ),
            getItem(Icons.share, "分享好友", () {
              Share.share('https://apps.apple.com/cn/app/id6451178132');
            }),
            getItem(Icons.security, "隐私条款", () {
              Navigator.push(context,
                  CupertinoPageRoute(builder: (BuildContext context) {
                return PrivacyPage();
              }));
            }),
            getItem(Icons.verified, "用户协议", () {
              Navigator.push(context,
                  CupertinoPageRoute(builder: (BuildContext context) {
                return FuwuPage();
              }));
            }),
            getItem(Icons.help, "关于", () {
              Utils.showToast("Version 1.0");
            }),
          ],
        ),
      ),
    );
  }

  getItem(IconData icon, String title, VoidCallback voidCallback) {
    return InkWell(
      onTap: () {
        voidCallback();
      },
      child: Container(
        margin: const EdgeInsets.fromLTRB(16, 8, 16, 8),
        width: MediaQuery.of(context).size.width - 32,
        height: 42,
        child: Row(
          children: [
            Icon(
              icon,
              size: 24,
              color: Colors.black,
            ),
            Container(
              width: 12,
            ),
            Text(
              title,
              style: const TextStyle(
                  color: Colors.black,
                  fontSize: 15,
                  fontWeight: FontWeight.w600),
            ),
            Expanded(child: Container()),
            const Icon(
              Icons.arrow_forward_ios,
              size: 16,
              color: Colors.black,
            )
          ],
        ),
      ),
    );
  }
}
